/*
 */

import { createStore } from "redux";

//  3. 提取初始值为单独的变量
// 👍推荐就叫initState
const initState = { count: 0, msg: "React" };

//  1. 提取reducer函数
function reducer(state = initState, action) {
  console.warn("进入仓库了  ----->  ", action);

  //  2. 使用swtich语句替换if语句
  switch (action.type) {
    case "add":
      return {
        ...state,
        count: state.count + 1,
      };
    case "des":
      return {
        ...state,
        count: state.count - 1,
      };
    default:
      return state;
  }
}

// 1.  把reducer函数提取为单的函数
// 👍通常, 函数名就叫reducer

//  2. 使用swtich语句替换if语句
// 💥 记得给default分支，写上返回值 state

const store = createStore(reducer);

store.dispatch({ type: "add" });
store.dispatch({ type: "des" });

// subscribe要在开启后, dispatch才能触发, 开启前dispatch不触发
const unFn = store.subscribe(() => {
  console.log("store改变了", store.getState());
});

store.dispatch({ type: "add" });

// 2 关闭监听
// 调用unFn就是关闭监听
unFn();
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
